package de.fhb.itunes.controller.web;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import de.fhb.itunes.manager.Datenverwaltung.Albenverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Musikverwaltung;

/**
 * Servlet implementation class albumcover
 */
@WebServlet("/albumcover")
public class albumcover extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public albumcover() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
		Enumeration en = request.getParameterNames();
		String value = "";
		while (en.hasMoreElements()) {
			String paramName = (String) en.nextElement();
			String paraValue = request.getParameter(paramName);
			if (paramName.contains("id")) {
				value = paraValue;
			}
		}
		if (value != "") {
			int id = Integer.parseInt(value);
			Albenverwaltung musik = Albenverwaltung.getInstance();
			byte[] daten = musik.getCoverByMusikID(id);
			if (daten != null) {
				OutputStream out = response.getOutputStream();
				response.setHeader("Content-Type", "image/jpg");
				out.write(daten);
				out.flush();
				out.close();
			}

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
